package com.yonyougov.devops.cruxcisupport.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;


@Component
public class MailUtils {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final JavaMailSender mailSender;

    public MailUtils(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * 发送文本邮件
     * @param from       发件人
     * @param tos        多个收件人
     * @param subject    主题
     * @param content    内容
     */
    public boolean sendTextMail(String from , String[] tos, String subject, String content){
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(tos);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            mailSender.send(simpleMailMessage);
            logger.info("send email successfully");
            logger.info("temp file delete successfully");
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.error("when send email get exception is : " + sw.toString());
            return false;
        }
        return true;
    }

    /**
     * 发送带附件的邮件
     * @param from       发件人
     * @param tos        多个收件人
     * @param subject    主题
     * @param content    内容
     * @param filePath  附件路径 支持一个
     */
    public boolean sendEmailWithAttachments(String from , String[] tos, String subject, String content, String filePath){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
            messageHelper.setFrom(from);
            messageHelper.setTo(tos);
            messageHelper.setSubject(subject);
            messageHelper.setText(content);
            File file = new File(filePath);
            String fileName = file.getName();
            messageHelper.addAttachment(fileName,file);
            mailSender.send(mimeMessage);
            logger.info("send email successfully");
        } catch (MessagingException e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.error("when send email get exception is : " + sw.toString());
            return false;
        }
        return true;
    }
    /**
     * 发送带附件的邮件
     * @param from       发件人
     * @param tos        多个收件人
     * @param subject    主题
     * @param content    内容
     * @param filePaths  附件路径 支持多个
     */
    public boolean sendEmailWithAttachments(String from , String[] tos, String subject, String content, List<String> filePaths){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
            messageHelper.setFrom(from);
            messageHelper.setTo(tos);
            messageHelper.setSubject(subject);
            messageHelper.setText(content);
            if(null != filePaths && filePaths.size()>0){
                for (String filePath : filePaths) {
                    File file = new File(filePath);
                    String fileName = file.getName();
                    messageHelper.addAttachment(fileName,file);
                }
            }
            mailSender.send(mimeMessage);
            logger.info("send email successfully");
        } catch (MessagingException e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.error("when send email get exception is : " + sw.toString());
            return false;
        }
        return true;
    }
}